package com.yanxu.stopthreads;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/7/21-2024
 * @Description: 阻塞的情况下，正确停止线程的方法
 */
public class RightWayStopThreadWithSleep {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                int num = 0;
                try {
                    while (num < 10000 && !Thread.currentThread().isInterrupted()) {
                        if (num % 100 == 0) {
                            System.out.println(num + "是100的倍数");
                        }
                        num++;
                    }
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();
        Thread.sleep(500);
        thread.interrupt();
    }
}